package com.util.config;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2021/11/4
 */
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Objects;


@ApiModel(
        description = "通用响应结构"
)
public class HttpResult<T> implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(HttpResult.class);
    private static final long serialVersionUID = -105653695825880271L;
    @ApiModelProperty("响应数据")
    T data;
    @ApiModelProperty("状态码")
    String code;
    @ApiModelProperty("错误消息")
    String msg;
    @ApiModelProperty("系统异常信息")
    String errorMsg;
    @ApiModelProperty("调用链追踪id")
    String traceId;
    @ApiModelProperty("调用链追踪id(兼容)")
    String requestId;
    /** @deprecated */
    @ApiModelProperty("响应时间，单位ms")
    @Deprecated
    Integer rt;
    @ApiModelProperty("客户端ip")
    String clientIp;

    public HttpResult() {
        this.code = "";
        this.msg = "";
        this.errorMsg = "";
        this.traceId = "";
        this.requestId = "";
        this.rt = 0;
    }

    private HttpResult(T data) {
        this(data, HttpResult.CodeTable.SUCCESS, "");
    }

    private HttpResult(T data, String code, String msg) {
        this.code = "";
        this.msg = "";
        this.errorMsg = "";
        this.traceId = "";
        this.requestId = "";
        this.rt = 0;
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public static <T> HttpResult<T> succ(T data) {
        return new HttpResult(data);
    }

    public static <T> HttpResult<T> fail(T data, String msg) {
        return new HttpResult(data, HttpResult.CodeTable.FAIL, msg);
    }

    public static <T> HttpResult<T> fail(String msg) {
        return fail(null, (String)msg);
    }



    public static <T> HttpResult<T> build(T data, String code, String msg) {
        return new HttpResult(data, code, msg);
    }



    @ApiModelProperty(
            hidden = true
    )
    public boolean isSuccess() {
        return StringUtils.equalsIgnoreCase(HttpResult.CodeTable.SUCCESS, this.code);
    }

    @ApiModelProperty(
            hidden = true
    )
    @JsonIgnore
    public T getIgnoreError() {
        if (this.isSuccess()) {
            return this.data;
        } else {
            log.debug("rpc error be ignored. result: {}", this.toString());
            return null;
        }
    }

    public String toString() {
        return "HttpResult{data=" + this.data + ", code=" + this.code + ", msg='" + this.msg + '\'' + ", errorMsg='" + this.errorMsg + '\'' + ", traceId='" + this.traceId + '\'' + ", requestId='" + this.requestId + '\'' + '}';
    }

    public T getData() {
        return this.data;
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getErrorMsg() {
        return this.errorMsg;
    }

    public String getTraceId() {
        return this.traceId;
    }

    public String getRequestId() {
        return this.requestId;
    }

    /** @deprecated */
    @Deprecated
    public Integer getRt() {
        return this.rt;
    }

    public String getClientIp() {
        return this.clientIp;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    /** @deprecated */
    @Deprecated
    public void setRt(Integer rt) {
        this.rt = rt;
    }

    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof HttpResult)) {
            return false;
        } else {
            HttpResult<?> other = (HttpResult)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label107: {
                    Object this$data = this.getData();
                    Object other$data = other.getData();
                    if (this$data == null) {
                        if (other$data == null) {
                            break label107;
                        }
                    } else if (this$data.equals(other$data)) {
                        break label107;
                    }

                    return false;
                }

                Object this$code = this.getCode();
                Object other$code = other.getCode();
                if (this$code == null) {
                    if (other$code != null) {
                        return false;
                    }
                } else if (!this$code.equals(other$code)) {
                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                label86: {
                    Object this$errorMsg = this.getErrorMsg();
                    Object other$errorMsg = other.getErrorMsg();
                    if (this$errorMsg == null) {
                        if (other$errorMsg == null) {
                            break label86;
                        }
                    } else if (this$errorMsg.equals(other$errorMsg)) {
                        break label86;
                    }

                    return false;
                }

                label79: {
                    Object this$traceId = this.getTraceId();
                    Object other$traceId = other.getTraceId();
                    if (this$traceId == null) {
                        if (other$traceId == null) {
                            break label79;
                        }
                    } else if (this$traceId.equals(other$traceId)) {
                        break label79;
                    }

                    return false;
                }

                label72: {
                    Object this$requestId = this.getRequestId();
                    Object other$requestId = other.getRequestId();
                    if (this$requestId == null) {
                        if (other$requestId == null) {
                            break label72;
                        }
                    } else if (this$requestId.equals(other$requestId)) {
                        break label72;
                    }

                    return false;
                }

                Object this$rt = this.getRt();
                Object other$rt = other.getRt();
                if (this$rt == null) {
                    if (other$rt != null) {
                        return false;
                    }
                } else if (!this$rt.equals(other$rt)) {
                    return false;
                }

                Object this$clientIp = this.getClientIp();
                Object other$clientIp = other.getClientIp();
                if (this$clientIp == null) {
                    if (other$clientIp != null) {
                        return false;
                    }
                } else if (!this$clientIp.equals(other$clientIp)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof HttpResult;
    }


    @Override
    public int hashCode() {
        return Objects.hash(data, code, msg, errorMsg, traceId, requestId, rt, clientIp);
    }

    @Data
    private static class CodeTable {
        public static final String SUCCESS;
        public static final String FAIL;

        private CodeTable() {
        }

        static {
            SUCCESS = "Success";
            FAIL = "FAIL";
        }
    }
}
